
###############################################################################################
# Plot of bivariate relationships between explanatory variables and shadow rapporteur selection
###############################################################################################

# Programme:  shdw-analysis02-bivariateRelations.r
# Project:    EP shadow rapporteur selection
# Author:		  Frank Haege, Department of Politics and Administration, University of Limerick
# Contact:		frank.haege@ul.ie

# Description
#############
# This script produces the bivariate plots reported in Appendix 1 of the article.
# Each plot shows the relationship between an explanatory variable and the outcome variable
# for the six different party groups. 
# The EFD group is excluded for the reasons stated in the article.
# The plots are vertically jittered scatterplots with a GAM smoother overlaid,
# except for the plot of the coincidence of national salience, 
# which a line plot of the difference in porportions


# Remove all objects
rm(list = ls(all = TRUE))

# Load libraries
library(data.table)
library(ggplot2)
library(ggpubr)

# Change default setting
options(stringsAsFactors = FALSE)
theme_set(theme_bw())
set.seed(7)

# Set working directory
setwd("C:\\Users\\Frank\\Dropbox\\European Union\\EP shadow rapporteurs\\EUP submission")

# Load data
load("Data analysis\\shdw-management05-clean.RData")

# Drop EFD observations
table(df$epg.accro, exclude=NULL)
df = df[df$epg.accro!="EFD", ]


# Plots of ideological distance of shadow to party group median
###############################################################

# Left-right dimension
p1 = ggplot(df, mapping=aes(x=diff.median.leftright, y=shadow)) +
  geom_jitter(shape=1, width=0, height=0.1) +
  geom_smooth() +
  scale_y_continuous(breaks=c(0,1),
                   labels=c("No", "Yes")) +
  facet_wrap(~epg.accro) +
  ylab("Shadow rapporteur") +
  xlab("Distance to median (Left-Right)") +
  theme(text=element_text(size=25))
p1
ggexport(p1, filename="Data analysis\\Figures\\shdw-analysis02a-diffmedianleftright.png", width=800, height=500)

# EU support dimension
p2 = ggplot(df, mapping=aes(x=diff.median.eu.position, y=shadow)) +
  geom_jitter(shape=1, width=0, height=0.1) +
  geom_smooth() +
  scale_y_continuous(breaks=c(0,1),
                     labels=c("No", "Yes")) +
  facet_wrap(~epg.accro) +
  ylab("Shadow rapporteur") +
  xlab("Distance to median (EU)") +
  theme(text=element_text(size=25))
p2
ggexport(p2, filename="Data analysis\\Figures\\shdw-analysis02b-diffmedianeu.png", width=800, height=500)

# GAL/TAN dimension
p3 = ggplot(df, mapping=aes(x=diff.median.galtan, y=shadow)) +
  geom_jitter(shape=1, width=0, height=0.1) +
  geom_smooth() +
  scale_y_continuous(breaks=c(0,1),
                     labels=c("No", "Yes")) +
  facet_wrap(~epg.accro) +
  ylab("Shadow rapporteur") +
  xlab("Distance to median (GAL/TAN)") +
  theme(text=element_text(size=25))
p3
ggexport(p3, filename="Data analysis\\Figures\\shdw-analysis02c-diffmediangaltan.png", width=800, height=500)


# Plots of ideological distance of shadow to rapporteur
#######################################################

# Left-right dimension
p4 = ggplot(df, mapping=aes(x=diff.rapp.leftright, y=shadow)) +
  geom_jitter(shape=1, width=0, height=0.1) +
  geom_smooth() +
  scale_y_continuous(breaks=c(0,1),
                     labels=c("No", "Yes")) +
  facet_wrap(~epg.accro) +
  ylab("Shadow rapporteur") +
  xlab("Distance to rapporteur (Left-Right)") +
  theme(text=element_text(size=25))
p4
ggexport(p4, filename="Data analysis\\Figures\\shdw-analysis02d-diffrappleftright.png", width=800, height=500)

# EU support dimension
p5 = ggplot(df, mapping=aes(x=diff.rapp.eu.position, y=shadow)) +
  geom_jitter(shape=1, width=0, height=0.1) +
  geom_smooth() +
  scale_y_continuous(breaks=c(0,1),
                     labels=c("No", "Yes")) +
  facet_wrap(~epg.accro) +
  ylab("Shadow rapporteur") +
  xlab("Distance to rapporteur (EU)") +
  theme(text=element_text(size=25))
p5
ggexport(p5, filename="Data analysis\\Figures\\shdw-analysis02e-diffrappeu.png", width=800, height=500)

# GAL/TAN dimension
p6 = ggplot(df, mapping=aes(x=diff.rapp.galtan, y=shadow)) +
  geom_jitter(shape=1, width=0, height=0.1) +
  geom_smooth() +
  scale_y_continuous(breaks=c(0,1),
                     labels=c("No", "Yes")) +
  facet_wrap(~epg.accro) +
  ylab("Shadow rapporteur") +
  xlab("Distance to rapporteur (GAL/TAN)") +
  theme(text=element_text(size=25))
p6
ggexport(p6, filename="Data analysis\\Figures\\shdw-analysis02f-diffrappgaltan.png", width=800, height=500)


# Plot of similarity in policy interests
########################################

p7 = ggplot(df, mapping=aes(x=ccc.all, y=shadow)) +
  geom_jitter(shape=1, width=0, height=0.1) +
  geom_smooth() +
  facet_wrap(~epg.accro) +
  scale_y_continuous(breaks=c(0,1),
                   labels=c("No", "Yes")) +
  ylab("Shadow rapporteur") +
  xlab("Similarity of policy interests") +
  theme(text=element_text(size=25))
p7
ggexport(p7, filename="Data analysis\\Figures\\shdw-analysis02g-simpolicy.png", width=800, height=500)


# Plot of coincidence in national salience
##########################################

# Create dataframe for plotting difference in proportions of shadow rapporteurs
ct = df[, c("shadow", "joint.country", "epg.accro")]
ct$one = 1
ct = data.table(ct)
ct = ct[, joint.country.total := .N, by=c("epg.accro", "joint.country")]
ct = ct[ct$shadow==1, ]
ct = ct[, .(prob.shadow = sum(shadow)/mean(joint.country.total)), by=c("epg.accro", "joint.country")]
ct = data.frame(ct)
ct = ct[order(ct$epg.accro, ct$joint.country), ]

p8 = ggplot(ct, mapping=aes(x=joint.country, y=prob.shadow)) +
  geom_line() +
  facet_wrap(~epg.accro) +
  scale_y_continuous(limits=c(0,1)) +
  scale_x_continuous(breaks=c(0,1),
                     labels=c("No", "Yes")) +
  ylab("Proportion of shadow rapporteurs") +
  xlab("Coincidence of national salience") +
  theme(text=element_text(size=25))
p8
ggexport(p8, filename="Data analysis\\Figures\\shdw-analysis02h-coincnational.png", width=800, height=500)
